package com.warrior.controller;

import com.warrior.configer.ConfigHelper;
import com.warrior.entity.ArticleEntity;
import com.warrior.service.ArticleService;
import com.warrior.util.CModelAndView;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 文章控制器
 * Created by Shawn on 2017/3/27.
 */
@Controller
@RequestMapping("/article")
public class ArticleController {

    private static final Logger logger = LogManager.getLogger(ArticleController.class.getName());

    @Autowired
    private ArticleService articleService;

    /**
     * 得到赛事详情规则
     * 根据id获取文章内容
     * @return ModelAndView
     */
    @RequestMapping(value = "/rule_details",method = RequestMethod.GET)
    public ModelAndView articleDetails( HttpServletRequest request, HttpServletResponse response){
        if (logger.isDebugEnabled()) {
            logger.debug("articleDetails(HttpServletRequest request={}, HttpServletResponse response={}) - start",request, response); //$NON-NLS-1$
        }

        ModelAndView mv = new CModelAndView("/warrior/activity/rules",request,response);
        String id = ConfigHelper.getConfig().getString("rules.activity");
        ArticleEntity articleEntity = this.articleService.findArticleById(id);
        mv.addObject("rules",articleEntity);

        if (logger.isDebugEnabled()) {
            logger.debug("articleDetails(String, HttpServletRequest, HttpServletResponse) - end"); //$NON-NLS-1$
        }
        return mv;
    }


    /**
     * 获取创建队伍的规则
     * @param request request
     * @param response response
     * @return ModelAndView
     */
    @RequestMapping(value = "/create_team_rule",method = RequestMethod.GET)
    public ModelAndView teamRules(HttpServletRequest request, HttpServletResponse response){
        ModelAndView mv = new CModelAndView("warrior/team/team_rule",request,response);
        ArticleEntity articleEntity = this.articleService.findArticleById(ConfigHelper.getConfig().getString("rules.create"));
        mv.addObject("createRule",articleEntity);
        return  mv;
    }


    /**
     * 获取转详细战队规则
     * @param request request
     * @param response response
     * @return ModelAndView
     */
    @RequestMapping(value = "/proteam_rule",method = RequestMethod.GET)
    public ModelAndView proteamRule(HttpServletRequest request, HttpServletResponse response){
        ModelAndView mv = new CModelAndView("warrior/team/proteam_rule",request,response);
        ArticleEntity articleEntity = this.articleService.findArticleById(ConfigHelper.getConfig().getString("rules.proteam_rule"));
        mv.addObject("proteamRule",articleEntity);
        return mv;
    }


    /**
     * 获取用户协议
     * @param request request
     * @param response response
     * @return ModelAndView
     */
    @RequestMapping(value = "/user_protocol",method = RequestMethod.GET)
    public ModelAndView userProtocol(HttpServletRequest request, HttpServletResponse response){
        ModelAndView mv = new CModelAndView("/",request,response);
        ArticleEntity articleEntity = this.articleService.findArticleById(ConfigHelper.getConfig().getString("rules.user_protocol"));
        mv.addObject("userProtocol",articleEntity);
        return mv;
    }






}
